package input;

import instance.Instance;
import instance.InstanceEj3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

public class Problem3Input implements Input {
	
	private String inputFile;
	private List<Instance> instances;
	
	public Problem3Input(String inputFile) {
		this.inputFile = inputFile;
	}
	//nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
		@Override
		public void readFile() throws Exception {
					
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					new File(inputFile)));

			instances = new ArrayList<Instance>();

			while (bufferedReader.ready()) {
				Instance instance =  readInstance(bufferedReader);
				instances.add( instance);
			}
				
			bufferedReader.close();		
		}

		@Override
		public List<Instance> getInstances() {
		
			return instances;
		}

		@Override
		public Instance readInstance(BufferedReader bufferedReader)
				throws Exception {
			int p;
			int q;
			String line = bufferedReader.readLine();
			StringTokenizer tokens = new StringTokenizer(line);
			
			//leemos primera linea,p y q
			p = Integer.parseInt(tokens.nextToken());
			q = Integer.parseInt(tokens.nextToken());
			
			//leemos segunda linea, posiciones de las piedras		
			Vector<Integer> list_piedras = new Vector<Integer>();
			line = bufferedReader.readLine();
			tokens = new StringTokenizer(line);
			
			while(tokens.hasMoreTokens()){
				list_piedras.add(Integer.parseInt(tokens.nextToken().trim()));	
				
			}
			//leemos tercer linea, pares x y
			LinkedList<Integer> tokenPares = new LinkedList<Integer>();
			line = bufferedReader.readLine();
			tokens = new StringTokenizer(line,";");
			
			while(tokens.hasMoreTokens()) {
				String par = tokens.nextToken();
				StringTokenizer  parx_y= new StringTokenizer(par);
				
				int x = Integer.parseInt(parx_y.nextToken().trim());
				int y = Integer.parseInt(parx_y.nextToken().trim());
				tokenPares.add(x);
				tokenPares.add(y);
			}
			Instance instance =  new InstanceEj3(p,q,list_piedras,tokenPares);
			return instance;
			}
			
	

}
